#$dir ="/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_emerge/clean/beagle/run_beagle/programtest";
#$file = "$dir/whole_gprobs";
#$file_tped = "$dir/whole_geno_1_22_x_nosnphapmap_nohapmapsnpsInital_check_ambi1.tped";
#$femalefile = "$dir/23/female/beagle_out_female.geno_female.bgl.gz.gprobs";
#$malefile = "$dir/23/male/beagle_out_male.geno_male.bgl.gz.phased";
#!/usr/bin/perl
my $file = $ARGV[0];
chomp $file;
my $dir = $ARGV[1];
chomp($dir);
my $newtped = $ARGV[2];
chomp($newtped);
if($file eq "")
{
        die "enter whole gprobs filename is empty\n";
}
if($dir eq "")
{
        die "entered outputfile is empty\n";
}
if($newtped eq "")
{
        die "entered newtpedfile is empty\n";
}

open BUFF, "< $file" or die "Cant open $file";
open BUFF1,"< $newtped" or die "Cant open $newtped";
$line = <BUFF>;
@array_ids = split(/\s/,$line);
$line = "SNP A1 A2";
for($z=3;$z<@array_ids;$z++)
{
	$array_sam[$num] = $array_ids[$z];
	$num++;
	$line = $line." $array_ids[$z]";
	$z++; 
	$z++;
}
open(WRBUFF,">$dir");

while($line = <BUFF>)
{
			
	chomp($line);
	@array = split(/\s/,$line);
	$line = $array[0]." ".$array[1]." ".$array[2];
	$hash_main{$array[0]} = 10;
	for($e =3;$e<@array;$e++)
	{
=head
		$e++;
		$tem1 = $array[$e];
	#	$line = $line." ".$array[$e];
		$e++;
		$tem2 = $array[$e];
		if($tem2 eq "-")
		{
			$tem3 =$tem2;
		}
		else
		{
			$tem3 = 2*$tem2+$tem1;
		}
		$line = $line." ".$tem3;
	#	$line = $line." ".$array[$e];
		#$e++;
=cut
		$tem1 = $array[$e];
		$e++;
		$tem2 = $array[$e];
		$e++;
		#$e++;
		$line = $line." ".$tem1." ".$tem2;
	}
	#opening new tped file line in parallel
=head
	$line_tped = <BUFF1>;
	@tped_arr = split(" ",$line_tped);
	shift(@tped_arr);
	shift(@tped_arr);
	shift(@tped_arr);
	shift(@tped_arr);
	$line_tped =join(" ",@tped_arr);
	$k =0;
	if($line_tped =~ m/A/)
	{
		$k++;	
		$gnt ="A";
	}
	if($line_tped =~ m/C/)
        {
                $k++;
		$gnt ="C";
        }
	if($line_tped =~ m/G/)
        {
                $k++;
		$gnt ="G";
        }
	if($line_tped =~ m/T/)
        {
                $k++;
		$gnt ="T";
        }
	if($k == 1)
	{
		@array = split(/\s/,$line);
		$array[1] = $gnt;
		$array[2] = "-";
		$line =join(" ",@array);
	}
=cut
	print WRBUFF $line."\n";
	
}
